package com.example.goods.service;

import com.example.common.AliyunUploadDown;
import com.example.goods.dao.GoodsMapper;
import com.example.goods.entity.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 16:10 2019/4/3
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    private static final int DEFAULT_LIMIT = 2;
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public void publishPost(Goods goods) {
        try {
         String imgUrl=AliyunUploadDown.picOSS(goods.getFile());
         goods.setGoodsImage(imgUrl);
         goods.setGoodsStatus((byte) 0);
         goods.setGoodsType((byte) 1);
         goods.setGoodsCreateTime(new Date());
         goodsMapper.insertSelective(goods);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Goods> goodsList() {
        return goodsMapper.selectGoodList();
    }

    @Override
    public List<Goods> goodsPaginationList(int offset) {
        if (offset!=0){
            offset=offset-1;
        }
      List<Goods> goodsList=goodsMapper.selectPaginationGoodList(offset*DEFAULT_LIMIT,DEFAULT_LIMIT);
        return goodsList;
    }

    @Override
    public int goodsListCount() {
        if (goodsMapper.selectGoodListCount()%DEFAULT_LIMIT==0){
            return (goodsMapper.selectGoodListCount()/DEFAULT_LIMIT);
        }else {
            return (goodsMapper.selectGoodListCount()/DEFAULT_LIMIT)+1;
        }

    }

    @Override
    public Goods selectGoodsDetail(Long goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public List<Goods> collectGoodsList() {
        return goodsMapper.selectCollectGoodList();
    }

    @Override
    public void publishCollectPost(Goods goods) {
        try {
            String imgUrl=AliyunUploadDown.picOSS(goods.getFile());
            goods.setGoodsImage(imgUrl);
            goods.setGoodsStatus((byte) 0);
            goods.setGoodsType((byte) 2);
            goods.setGoodsCreateTime(new Date());
            goodsMapper.insertSelective(goods);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Goods> collectGoodsPaginationList(Integer offset) {
        if (offset!=0){
            offset=offset-1;
        }
        List<Goods> goodsList=goodsMapper.selectCollectPaginationGoodList(offset*DEFAULT_LIMIT,DEFAULT_LIMIT);
        return goodsList;
    }

    @Override
    public int collectGoodsListCount() {
        if (goodsMapper.selectCollectGoodListCount()%DEFAULT_LIMIT==0){
            return (goodsMapper.selectCollectGoodListCount()/DEFAULT_LIMIT);
        }else {
            return (goodsMapper.selectCollectGoodListCount()/DEFAULT_LIMIT)+1;
        }
    }

    @Override
    public List<Goods> selectGoodsListPage(int offset, int limit) {
        return goodsMapper.selectGoodsListPage(offset,limit);
    }

    @Override
    public int selectGoodsPageCount() {
        return goodsMapper.selectGoodsPageCount();
    }

    @Override
    public void publishById(Goods goods) {
        goods.setGoodsStatus((byte) 1);
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public void deleteGoods(Goods goods) {
        goodsMapper.deleteByPrimaryKey(goods.getGoodId());
    }
}
